/** 
 * File-Name:ProjectAction.java
 *
 * Created on 2011-6-2 下午10:33:31
 * 
 * @author: Neo (neolimeng@gmail.com)
 * Software Engineering Institute, Peking University, China
 * 
 * Copyright (c) 2009, Peking University
 * 
 *
 */
package xiaoyou.web;

import java.io.File;
import java.io.InputStream;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import xiaoyou.entity.Project;
import xiaoyou.entity.User;
import xiaoyou.neoutil.CommonAction;
import xiaoyou.neoutil.CommonService;
import xiaoyou.neoutil.FileUtils;
import xiaoyou.neoutil.Page;
import xiaoyou.service.ProjectRepository;

/**
 * Description:
 * 
 * @author: Neo (neolimeng@gmail.com) Software Engineering Institute, Peking
 *          University, China
 * @version 1.0 2011-6-2 下午10:33:31
 */
public class ProjectAction extends CommonAction {

	private static final long serialVersionUID = 5856447211037433919L;
	private static Logger log = Logger.getLogger(ProjectAction.class);
	private Project project;
	private Page<Project> page = new Page<Project>(10);
	private File attachmentFile;
	private String attachmentFileFileName;
	private String attachmentFileContentType;
	private String fileSavePath;

	public String addProject() {
		try {
			User user = (User) getSessionObject("loginUser");
			if (user == null) {
				log.error("用户未登录！");
				return RELOGIN;
			} else {
				project.setAuthor(user);

				if (attachmentFile != null) {
					String fileName = project.getUuid()
							+ FileUtils
									.getExtention(getAttachmentFileFileName());
					FileUtils.write(getFileSavePath() + "\\", fileName,
							getAttachmentFile());

					project.setReserve01(fileName);
				}

				if (CommonService.SUCCESS == getProjectRepository().save(project)) {
					return SUCCESS;
				} else {
					return INPUT;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public File getAttachmentFile() {
		return attachmentFile;
	}

	public void setAttachmentFile(File attachmentFile) {
		this.attachmentFile = attachmentFile;
	}

	public String getAttachmentFileFileName() {
		return attachmentFileFileName;
	}

	public void setAttachmentFileFileName(String attachmentFileFileName) {
		this.attachmentFileFileName = attachmentFileFileName;
	}

	public String getAttachmentFileContentType() {
		return attachmentFileContentType;
	}

	public void setAttachmentFileContentType(String attachmentFileContentType) {
		this.attachmentFileContentType = attachmentFileContentType;
	}

	public String toEditProject() {
		try {
			String uuid = getRequestParameter("uuid");
			User user = (User) getSessionObject("loginUser");
			if (user == null) {
				log.error("用户未登录！");
				return RELOGIN;
			} else if (uuid == null || "".equals(uuid)) {
				return INPUT;
			} else {
				ProjectRepository projectRepository = new ProjectRepository();
				project = projectRepository.get(uuid);
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String editProject() {
		try {
			User user = (User) getSessionObject("loginUser");
			if (user == null) {
				log.error("用户未登录！");
				return RELOGIN;
			} else {
				ProjectRepository projectRepository = new ProjectRepository();
				project.setAuthorId(user.getId());
				
				if (attachmentFile != null) {
					String fileName = project.getUuid()
							+ FileUtils
									.getExtention(getAttachmentFileFileName());
					FileUtils.write(getFileSavePath() + "\\", fileName,
							getAttachmentFile());

					project.setReserve01(fileName);
				}
				
				if (CommonService.SUCCESS == projectRepository.update(project)) {
					return SUCCESS;
				} else {
					return INPUT;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}
	public String downloadProjectEnclosure() {
		return SUCCESS;
	}
	
	public InputStream getEnclosureFile() {
		try {
			String uuid = getRequestParameter("uuid");
			project = getProjectRepository().get(uuid);

			return new java.io.FileInputStream(getFileSavePath() + "\\"
					+ project.getReserve01());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public String deleteProject() {
		try {
			User user = (User) getSessionObject("loginUser");
			long projectId = Long.parseLong(getRequestParameter("id"));
			if (user == null) {
				log.error("用户未登录！");
				return RELOGIN;
			} else {
				ProjectRepository projectRepository = new ProjectRepository();
				if (CommonService.SUCCESS == projectRepository
						.delete(projectId)) {
					return SUCCESS;
				} else {
					return INPUT;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String getProjectByUuid() {
		try {
			String uuid = getRequestParameter("uuid");
			if (uuid == null || "".equals(uuid)) {
				return INPUT;
			} else {
				ProjectRepository projectRepository = new ProjectRepository();
				project = projectRepository.get(uuid);
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String getAllProjects() {
		try {
			ProjectRepository projectRepository = new ProjectRepository();
			page = projectRepository.getProjects(page);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public Page<Project> getPage() {
		return page;
	}

	public void setPage(Page<Project> page) {
		this.page = page;
	}


	public String getFileSavePath() {
		return ServletActionContext.getRequest().getRealPath(fileSavePath);
	}

	public void setFileSavePath(String fileSavePath) {
		this.fileSavePath = fileSavePath;
	}

}
